package com.iterator.service.wechat.dhy;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iterator.bean.web.pojo.Brand;
import com.iterator.bean.web.pojo.BrandExample;
import com.iterator.bean.web.pojo.dhy.KeywordExample;
import com.iterator.bean.web.pojo.lyh.User;
import com.iterator.bean.web.pojo.tcy.*;
import com.iterator.bean.web.pojo.zql.*;
import com.iterator.bean.web.wy.issue.Issue;
import com.iterator.bean.web.wy.issue.IssueExample;
import com.iterator.bean.wechat.dhy.*;

import com.iterator.bean.wechat.lc.po.SearchHistory;
import com.iterator.mapper.web.configManager.ConfigMapper;
import com.iterator.mapper.web.goodsManager.*;
import com.iterator.mapper.web.marketManager.BrandMapper;
import com.iterator.mapper.web.marketManager.IssueMapper;
import com.iterator.mapper.web.marketManager.KeywordMapper;
import com.iterator.mapper.web.promotionMapper.CouponMapper;
import com.iterator.mapper.web.promotionMapper.GrouponRulesMapper;
import com.iterator.mapper.web.promotionMapper.TopicMapper;

import com.iterator.mapper.web.spreadManager.AdMapper;
import com.iterator.mapper.web.userManager.MallUserMapper;
import com.iterator.mapper.wechat.dhy.WxMallCategoryMapper;
import com.iterator.mapper.wechat.lc.CustomSearchHistoryMapper;
import com.iterator.mapper.wechat.lc.WxKeyWordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.regex.Pattern;

/**
 * @Author: DHY
 * @Date: 2021/1/12 15:36
 * @Description:
 */
@Service
public class WxGoodsServiceImpl implements WxGoodsService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    WxMallCategoryMapper mallCategoryMapper;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    GrouponRulesMapper grouponRulesMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    AdMapper adMapper;
    @Autowired
    TopicMapper topicMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    MallUserMapper mallUserMapper;
    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    IssueMapper issueMapper;
    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    ConfigMapper configMapper;
    @Autowired
    CustomSearchHistoryMapper customSearchHistoryMapper;

    /**
     * 显示商品数量
     * @return
     */
    @Override
    public Map<String, Integer> showGoodsCount() {
        Integer count = goodsMapper.countByExample(new GoodsExample());
        Map<String, Integer> map = new HashMap<>();
        map.put("goodsCount",count);
        return map;
    }

    /**
     * 显示商品信息页面
     * @param id
     * @return
     */
    @Override
    public GoodsCategoryVO showCategory(Integer id) {
        GoodsCategoryVO goodsCategoryVO = new GoodsCategoryVO();
        WxMallCategoryExample wxMallCategoryExample = new WxMallCategoryExample();
        WxMallCategoryExample.Criteria criteria = wxMallCategoryExample.createCriteria();
        //判断当前id是类id还是商品id

        WxMallCategory wxMallCategory2 = mallCategoryMapper.selectByPrimaryKey(id);
        if (wxMallCategory2.getPid() != 0) {
            //是商品id
            //查询当前商品信息
            WxMallCategory wxMallCategory = mallCategoryMapper.selectByPrimaryKey(id);
            goodsCategoryVO.setCurrentCategory(wxMallCategory);
            //查询所属商品类别
            WxMallCategory wxMallCategory1 = mallCategoryMapper.selectByPrimaryKey(wxMallCategory.getPid());
            goodsCategoryVO.setParentCategory(wxMallCategory1);
            //查询当前类的商品信息

            criteria.andPidEqualTo(wxMallCategory1.getId());
            List<WxMallCategory> wxMallCategories = mallCategoryMapper.selectByExample(wxMallCategoryExample);
            goodsCategoryVO.setBrotherCategory(wxMallCategories);
            return goodsCategoryVO;
        } else {
            //是类id
            //查询当前类的商品信息
            criteria.andPidEqualTo(id);
            List<WxMallCategory> wxMallCategories = mallCategoryMapper.selectByExample(wxMallCategoryExample);
            goodsCategoryVO.setBrotherCategory(wxMallCategories);
            //封装类信息
            goodsCategoryVO.setParentCategory(wxMallCategory2);
            //封装当前商品信息
            WxMallCategory currentCategory=null;
            if (wxMallCategories.size()!=0){
                currentCategory= wxMallCategories.get(0);
            }
            goodsCategoryVO.setCurrentCategory(currentCategory);
            return goodsCategoryVO;
        }

    }

    /**
     * 商品显示
     * @param categoryId 商品所属类id
     * @param page 页
     * @param size 页大小
     * @param keyword 搜索的关键
     * @param sort 按什么排序
     * @param order 排序方式
     * @param brandId 品牌id
     * @return
     */
    @Override
    public GoodsListVO showList(Integer categoryId, Integer page, Integer size, String keyword, String sort, String order, Integer brandId, String username) {
        GoodsListVO goodsListVO = new GoodsListVO();
        PageHelper.startPage(page,size);
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        //Here changed by lc
        //查询用户id
        //TODO getPrincipal不能用?
        if(keyword!=null && keyword.length()!=0){
            String[] split= keyword.split("\\s+");
            addSearchHistory(username,split,criteria);
            addKeyWord(split);
        }


        //搜索词汇添加至History


        if (sort != null && order != null) {
            goodsExample.setOrderByClause(sort + " " + order);
        }
        //排序方式

        //模糊查询



        //种类id
        if (categoryId!=null&&categoryId != 0) {
            criteria.andCategoryIdEqualTo(categoryId);
        }
        //品牌id
        if (brandId != null && brandId != 0) {
            criteria.andBrandIdEqualTo(brandId);
        }
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goods);
        int total = (int) goodsPageInfo.getTotal();
        //封装
        List<GoodsListVO.GoodsListBean> goodsListBeans = getGoodsListBeans(goods);
        //封装查询的商品条数
        goodsListVO.setCount(total);
        //封装商品详情
        goodsListVO.setGoodsList(goodsListBeans);

        //查询类别信息
        List<Integer> integers = new ArrayList<>();
        for (GoodsListVO.GoodsListBean goodsListBean : goodsListBeans) {
            Goods goods1 = goodsMapper.selectByPrimaryKey(goodsListBean.getId());
            Integer categoryId1 = goods1.getCategoryId();
            if (!integers.contains(categoryId1)) {
                integers.add(categoryId1);
            }
        }
        WxMallCategoryExample wxMallCategoryExample = new WxMallCategoryExample();
        if (integers != null && integers.size() != 0) {
            wxMallCategoryExample.createCriteria().andIdIn(integers);
        }
        List<WxMallCategory> wxMallCategories = mallCategoryMapper.selectByExample(wxMallCategoryExample);
        //封装类别信息
        goodsListVO.setFilterCategoryList(wxMallCategories);
        return goodsListVO;
    }
    @Autowired
    WxKeyWordMapper wxKeyWordMapper;
    @Autowired
    KeywordMapper keywordMapper;
    private void addKeyWord(String[] split) {
        List<String> keywords = new ArrayList<>();
        for (String s : split) {
            keywords.add(s);
        }
        //插入keyword
        wxKeyWordMapper.insertBatch(keywords);
    }

    //搜索关键词时 修改criteria
    private void addSearchHistory(String username,String[] split,GoodsExample.Criteria criteria) {
        List<SearchHistory> keywords = new ArrayList<>();
        if(username!=null && (username!="")) {
            User user = mallUserMapper.selectUserByName(username);
            //批量插入History
            for (String s : split) {
                keywords.add(new SearchHistory(null, user.getId(), s, "wx", new Date(), new Date(), false));
            }
            customSearchHistoryMapper.insertBatch(keywords);
        }
        for (String keywordItem : split) {

            if (keywordItem != null && keywordItem.length() != 0) {
                criteria.andNameLike("%"+keywordItem+"%");
            }
        }
    }

    /**
     * 封装商品
     * @param goods
     * @return
     */
    private List<GoodsListVO.GoodsListBean> getGoodsListBeans(List<Goods> goods) {
        List<GoodsListVO.GoodsListBean> goodsListBeans = new ArrayList<>();
        for (Goods good : goods) {
            GoodsListVO.GoodsListBean goodsListBean = new GoodsListVO.GoodsListBean();
            goodsListBean.setId(good.getId());
            goodsListBean.setName(good.getName());
            goodsListBean.setPicUrl(good.getPicUrl());
            goodsListBean.setNew(good.getIsNew());
            goodsListBean.setHot(good.getIsHot());
            goodsListBean.setCounterPrice(good.getCounterPrice().doubleValue());
            goodsListBean.setRetailPrice(good.getRetailPrice().doubleValue());
            goodsListBeans.add(goodsListBean);
        }
        return goodsListBeans;
    }

    /**
     * 主页面显示
     * @return
     */
    @Override
    public IndexVO showIndex() {
        //查询主页配置信息
        List<IndexConfig> list= configMapper.selectConfigInfo();
        //遍历封装
        IndexConfigInfo indexConfigInfo = getIndexConfigInfo(list);
        IndexVO indexVO = new IndexVO();
        /**
         * 封装新商品信息 newGoodsList
         */
        //限时显示信息条数
        PageHelper.startPage(1,indexConfigInfo.getIndexNew());
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andIsNewEqualTo(true);
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        //封装
        List<IndexVO.NewGoodsListBean> newGoodsListBeans = getNewGoodsListBeans(goods);
        indexVO.setNewGoodsList(newGoodsListBeans);

        /**
         * 封装优惠券信息 couponList
         */
        //限时显示信息条数
        PageHelper.startPage(1,4);
        CouponExample example = new CouponExample();
        example.setOrderByClause("update_time  desc");
        List<Coupon> coupons = couponMapper.selectByExample(example);
        //封装
        List<IndexVO.CouponListBean> couponListBeans = getCouponListBeans(coupons);
        indexVO.setCouponList(couponListBeans);

        /**
         *封装类目信息
         */
        WxMallCategoryExample wxMallCategoryExample = new WxMallCategoryExample();
        WxMallCategoryExample.Criteria criteria1 = wxMallCategoryExample.createCriteria();
        criteria1.andPidEqualTo(0);
        List<WxMallCategory> wxMallCategories = mallCategoryMapper.selectByExample(wxMallCategoryExample);
        //封装
        List<IndexVO.ChannelBean> channelBeans = getChannelBeans(wxMallCategories);
        indexVO.setChannel(channelBeans);

        /**
         *封装团购信息
         */
        //限时显示信息条数
        PageHelper.startPage(1,4);
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(new GrouponRulesExample());
        //封装
        ArrayList<IndexVO.GrouponListBean> grouponListBeans = getGrouponListBeans(grouponRules);
        indexVO.setGrouponList(grouponListBeans);

        /**
         * 封装标语信息
         */
        List<Ad> ads = adMapper.selectByExample(new AdExample());
        indexVO.setBanner(ads);
        /**
         *封装品牌制造商信息
         */
        //限时显示信息条数
        PageHelper.startPage(1,indexConfigInfo.getIndexBrand());
        List<Brand> brands = brandMapper.selectByExample(new BrandExample());
        //封装
        List<IndexVO.BrandListBean> brandListBeans = getBrandListBeans(brands);
        indexVO.setBrandList(brandListBeans);

        /**
         * 封装热卖商品
         */
        //限时显示信息条数
        PageHelper.startPage(1,indexConfigInfo.getIndexHot());
        GoodsExample goodsExample1 = new GoodsExample();
        goodsExample1.createCriteria().andIsHotEqualTo(true);
        List<Goods> goods1 = goodsMapper.selectByExample(goodsExample1);
        //封装
        ArrayList<IndexVO.HotGoodsListBean> hotGoodsListBeans = getHotGoodsListBeans(goods1);
        indexVO.setHotGoodsList(hotGoodsListBeans);

        /**
         * 封装主题精选信息
         */
        PageHelper.startPage(1,indexConfigInfo.getIndexTopic());
        List<Topic> topics = topicMapper.selectByExample(new TopicExample());
        //封装
        List<IndexVO.TopicListBean> topicListBeans = getTopicListBeans(topics);
        indexVO.setTopicList(topicListBeans);

        /**
         *底部信息封装
         */
        //查询商品类信息
        WxMallCategoryExample wxMallCategoryExample1 = new WxMallCategoryExample();
        wxMallCategoryExample1.setOrderByClause("sort_order");
        WxMallCategoryExample.Criteria criteria2 = wxMallCategoryExample1.createCriteria().andPidNotEqualTo(0);
        List<WxMallCategory> wxMallCategories1 = mallCategoryMapper.selectByExample(wxMallCategoryExample1);
        List<IndexVO.FloorGoodsListBean> floorGoodsListBeans = new ArrayList<>();
        //判断是否有几个类别
        int top= wxMallCategories1.size()>indexConfigInfo.getCatlogList() ? indexConfigInfo.getCatlogList() : wxMallCategories1.size();
        for (int i = 0; i < top ; i++) {
            WxMallCategory wxMallCategory = wxMallCategories1.get(i);
            IndexVO.FloorGoodsListBean floorGoodsListBean = new IndexVO.FloorGoodsListBean();
            floorGoodsListBean.setId(wxMallCategory.getId());
            floorGoodsListBean.setName(wxMallCategory.getName());
            //根据类查询商品信息
            PageHelper.startPage(1,indexConfigInfo.getCatlogGoods());
            GoodsExample goodsExample2 = new GoodsExample();
            goodsExample2.createCriteria().andCategoryIdEqualTo(wxMallCategory.getId());
            List<Goods> goods2 = goodsMapper.selectByExample(goodsExample2);
            //封装
            List<IndexVO.FloorGoodsListBean.GoodsListBean> floorGoodsListBeans1 = getFloorGoodsListBeans(goods2);
            floorGoodsListBean.setGoodsList(floorGoodsListBeans1);
            floorGoodsListBeans.add(floorGoodsListBean);
        }
        indexVO.setFloorGoodsList(floorGoodsListBeans);
        return indexVO;
    }

    private IndexConfigInfo getIndexConfigInfo(List<IndexConfig> list) {
        IndexConfigInfo indexConfigInfo = new IndexConfigInfo();
        //遍历封装
        for (IndexConfig indexConfig : list) {
            //新品首发商品
            if (indexConfig.getKeyName().equals("cskaoyan_mall_wx_index_new")) {
            indexConfigInfo.setIndexNew(Integer.parseInt(indexConfig.getKeyValue()));
            }
            //人气栏目商品
            if (indexConfig.getKeyName().equals("cskaoyan_mall_wx_index_hot")) {
                indexConfigInfo.setIndexHot(Integer.parseInt(indexConfig.getKeyValue()));
            }
            //品牌制造商
            if (indexConfig.getKeyName().equals("cskaoyan_mall_wx_index_brand")) {
                indexConfigInfo.setIndexBrand(Integer.parseInt(indexConfig.getKeyValue()));
            }
            //专题精选栏目
            if (indexConfig.getKeyName().equals("cskaoyan_mall_wx_index_topic")) {
                indexConfigInfo.setIndexTopic(Integer.parseInt(indexConfig.getKeyValue()));
            }
            //分类栏目
            if (indexConfig.getKeyName().equals("cskaoyan_mall_wx_catlog_list")) {
                indexConfigInfo.setCatlogList(Integer.parseInt(indexConfig.getKeyValue()));
            }
            //分类栏目商品
            if (indexConfig.getKeyName().equals("cskaoyan_mall_wx_catlog_goods")) {
                indexConfigInfo.setCatlogGoods(Integer.parseInt(indexConfig.getKeyValue()));
            }
        }
        return indexConfigInfo;
    }

    private List<IndexVO.FloorGoodsListBean.GoodsListBean> getFloorGoodsListBeans(List<Goods> goods2) {
        List<IndexVO.FloorGoodsListBean.GoodsListBean> floorGoodsListBeans1 = new ArrayList<>();
        for (Goods goods3 : goods2) {
            IndexVO.FloorGoodsListBean.GoodsListBean goodsListBean = new IndexVO.FloorGoodsListBean.GoodsListBean();
            goodsListBean.setId(goods3.getId());
            goodsListBean.setName(goods3.getName());
            goodsListBean.setBrief(goods3.getBrief());
            goodsListBean.setPicUrl(goods3.getPicUrl());
            goodsListBean.setNew(goods3.getIsNew());
            goodsListBean.setHot(goods3.getIsHot());
            goodsListBean.setCounterPrice(goods3.getCounterPrice().intValue());
            goodsListBean.setRetailPrice(goods3.getRetailPrice().intValue());
            floorGoodsListBeans1.add(goodsListBean);
        }
        return floorGoodsListBeans1;
    }

    private List<IndexVO.TopicListBean> getTopicListBeans(List<Topic> topics) {
        List<IndexVO.TopicListBean> topicListBeans = new ArrayList<>();
        for (Topic topic : topics) {
            IndexVO.TopicListBean topicListBean = new IndexVO.TopicListBean();
            topicListBean.setId(topic.getId());
            topicListBean.setTitle(topic.getTitle());
            topicListBean.setSubtitle(topic.getSubtitle());
            topicListBean.setPrice(topic.getPrice().intValue());
            topicListBean.setReadCount(topic.getReadCount());
            topicListBean.setPicUrl(topic.getPicUrl());
            topicListBeans.add(topicListBean);
        }
        return topicListBeans;
    }

    private ArrayList<IndexVO.HotGoodsListBean> getHotGoodsListBeans(List<Goods> goods1) {
        ArrayList<IndexVO.HotGoodsListBean> hotGoodsListBeans = new ArrayList<>();
        for (Goods goods2 : goods1) {
            IndexVO.HotGoodsListBean hotGoodsListBean = new IndexVO.HotGoodsListBean();
            hotGoodsListBean.setId(goods2.getId());
            hotGoodsListBean.setName(goods2.getName());
            hotGoodsListBean.setBrief(goods2.getBrief());
            hotGoodsListBean.setPicUrl(goods2.getPicUrl());
            hotGoodsListBean.setNew(goods2.getIsNew());
            hotGoodsListBean.setHot(goods2.getIsHot());
            hotGoodsListBean.setCounterPrice(goods2.getCounterPrice().intValue());
            hotGoodsListBean.setRetailPrice(goods2.getRetailPrice().intValue());
            hotGoodsListBeans.add(hotGoodsListBean);
        }
        return hotGoodsListBeans;
    }

    private List<IndexVO.BrandListBean> getBrandListBeans(List<Brand> brands) {
        List<IndexVO.BrandListBean> brandListBeans = new ArrayList<>();
        for (Brand brand : brands) {
            IndexVO.BrandListBean brandListBean = new IndexVO.BrandListBean();
            brandListBean.setId(brand.getId());
            brandListBean.setName(brand.getName());
            brandListBean.setDesc(brand.getDesc());
            brandListBean.setPicUrl(brand.getPicUrl());
            brandListBean.setFloorPrice(brand.getFloorPrice().intValue());
            brandListBeans.add(brandListBean);
        }
        return brandListBeans;
    }

    private ArrayList<IndexVO.GrouponListBean> getGrouponListBeans(List<GrouponRules> grouponRules) {
        ArrayList<IndexVO.GrouponListBean> grouponListBeans = new ArrayList<>();
        for (GrouponRules grouponRule : grouponRules) {
            Integer goodsId = grouponRule.getGoodsId();
            //查询商品信息
            Goods goods1 = goodsMapper.selectByPrimaryKey(goodsId);
            IndexVO.GrouponListBean.GoodsBean goodsBean = new IndexVO.GrouponListBean.GoodsBean();
            goodsBean.setId(goods1.getId());
            goodsBean.setName(goods1.getName());
            goodsBean.setBrief(goods1.getBrief());
            goodsBean.setPicUrl(goods1.getPicUrl());
            goodsBean.setCounterPrice(goods1.getCounterPrice().intValue());
            goodsBean.setRetailPrice(goods1.getRetailPrice().intValue());

            Integer price = goods1.getCounterPrice().intValue() - grouponRule.getDiscount().intValue();
            IndexVO.GrouponListBean grouponListBean = new IndexVO.GrouponListBean();
                grouponListBean.setGroupon_price(price);
                grouponListBean.setGoods(goodsBean);
                grouponListBean.setGroupon_member(grouponRule.getDiscountMember());
            grouponListBeans.add(grouponListBean);
        }
        return grouponListBeans;
    }

    private List<IndexVO.ChannelBean> getChannelBeans(List<WxMallCategory> wxMallCategories) {
        List<IndexVO.ChannelBean> channelBeans = new ArrayList<>();
        for (WxMallCategory wxMallCategory : wxMallCategories) {
            IndexVO.ChannelBean channelBean = new IndexVO.ChannelBean();
            channelBean.setId(wxMallCategory.getId());
            channelBean.setName(wxMallCategory.getName());
            channelBean.setIconUrl(wxMallCategory.getIconUrl());
            channelBeans.add(channelBean);
        }
        return channelBeans;
    }

    private List<IndexVO.CouponListBean> getCouponListBeans(List<Coupon> coupons) {
        List<IndexVO.CouponListBean> couponListBeans = new ArrayList<>();
        for (Coupon coupon : coupons) {
            IndexVO.CouponListBean couponListBean = new IndexVO.CouponListBean();
            couponListBean.setId(coupon.getId());
            couponListBean.setName(coupon.getName());
            couponListBean.setDesc(coupon.getDesc());
            couponListBean.setTag(coupon.getTag());
            couponListBean.setDiscount(coupon.getDiscount().intValue());
            couponListBean.setMin(coupon.getMin().intValue());
            couponListBean.setDays(coupon.getDays());
            couponListBeans.add(couponListBean);
        }
        return couponListBeans;
    }

    private List<IndexVO.NewGoodsListBean> getNewGoodsListBeans(List<Goods> goods) {
        List<IndexVO.NewGoodsListBean> newGoodsListBeans = new ArrayList<>();
        for (Goods good : goods) {
            IndexVO.NewGoodsListBean newGoodsListBean = new IndexVO.NewGoodsListBean();
            newGoodsListBean.setId(good.getId());
            newGoodsListBean.setName(good.getName());
            newGoodsListBean.setBrief(good.getBrief());
            newGoodsListBean.setPicUrl(good.getPicUrl());
            newGoodsListBean.setNew(good.getIsNew());
            newGoodsListBean.setHot(good.getIsHot());
            newGoodsListBean.setCounterPrice(good.getCounterPrice().intValue());
            newGoodsListBean.setRetailPrice(good.getRetailPrice().intValue());
            newGoodsListBeans.add(newGoodsListBean);
        }
        return newGoodsListBeans;
    }

    /**
     * 商品详细信息显示
     * @param id
     * @return
     */
    @Override
    public DetailListVO showDetailList(Integer id) {
        DetailListVO detailListVO = new DetailListVO();
        detailListVO.setUserHasCollect(0);
        /**
         * 封装评论信息
         */
        DetailListVO.CommentBean commentBean = new DetailListVO.CommentBean();
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andValueIdEqualTo(id);
        //查询评论条数
        int count = (int) commentMapper.countByExample(commentExample);
        commentBean.setCount(count);
        //显示评论信息的最大条数
        PageHelper.startPage(1,4);
        //查询评论信息
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        //封装信息
        List<DetailListVO.CommentBean.CommentChildren> commentChildren = getCommentChildren(comments);
        //封装评论的list
        commentBean.setData(commentChildren);
        //封装评论信息
        detailListVO.setComment(commentBean);


        /**
         * 封装商品信息
         */
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        detailListVO.setInfo(goods);

        /**
         * 封装品牌信息
         */
        //获得商品品牌id
        Integer brandId = goods.getBrandId();
        DetailListVO.BrandBean brandBean = new DetailListVO.BrandBean();
        //判断是否属于品牌
        if (brandId!=0){
            //查询品牌信息
            Brand brand = brandMapper.selectByPrimaryKey(brandId);
            //封装
            brandBean.setId(brand.getId());
            brandBean.setName(brand.getName());
            brandBean.setDesc(brand.getDesc());
            brandBean.setPicUrl(brand.getPicUrl());
            brandBean.setSortOrder(brand.getSortOrder());
            brandBean.setFloorPrice(brand.getFloorPrice().intValue());
            brandBean.setAddTime(brand.getAddTime());
            brandBean.setUpdateTime(brand.getUpdateTime());
            brandBean.setDeleted(brand.getDeleted());
        }
        detailListVO.setBrand(brandBean);




        /**
         * 封装商品规格信息
         */
        GoodsSpecificationExample goodsSpecificationExample = new GoodsSpecificationExample();
        goodsSpecificationExample.createCriteria().andGoodsIdEqualTo(id);
        //查询规格
        List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.selectByExample(goodsSpecificationExample);
        //封装信息
        List<DetailListVO.SpecificationListBean> specificationListBeans = getSpecificationListBeans(goodsSpecifications);
        detailListVO.setSpecificationList(specificationListBeans);


        /**
         *封装团购信息
         */
        GrouponRulesExample grouponRules = new GrouponRulesExample();
        grouponRules.createCriteria().andGoodsIdEqualTo(id);
        //根据商品id查询相关团购信息
        List<GrouponRules> grouponRules1 = grouponRulesMapper.selectByExample(grouponRules);
        detailListVO.setGroupon(grouponRules1);

        /**
         * 封装问题信息
         */
        List<Issue> issues = issueMapper.selectByExample(new IssueExample());
        detailListVO.setIssue(issues);

        /**
         *封装商品特性
         */
        GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
        goodsAttributeExample.createCriteria().andGoodsIdEqualTo(id);
        List<GoodsAttribute> goodsAttributes = goodsAttributeMapper.selectByExample(goodsAttributeExample);
        detailListVO.setAttribute(goodsAttributes);


        /**
         * 封装商品型号信息
         */
        GoodsProductExample goodsProductExample = new GoodsProductExample();
        goodsProductExample.createCriteria().andGoodsIdEqualTo(id);
        List<GoodsProduct> goodsProducts = goodsProductMapper.selectByExample(goodsProductExample);
        detailListVO.setProductList(goodsProducts);

        return detailListVO;
    }

    private List<DetailListVO.SpecificationListBean> getSpecificationListBeans(List<GoodsSpecification> goodsSpecifications) {
        List<DetailListVO.SpecificationListBean> specificationListBeans = new ArrayList<>();
        for (GoodsSpecification goodsSpecification : goodsSpecifications) {
            DetailListVO.SpecificationListBean specificationListBean = new DetailListVO.SpecificationListBean();
            specificationListBean.setName(goodsSpecification.getSpecification());
            //里层list放入相同规格信息
            List<GoodsSpecification> goodsSpecifications1 = new ArrayList<>();
            for (GoodsSpecification specification : goodsSpecifications) {
                //相同类型的放在一个list里 例如  规格  颜色
                if (specification.getSpecification().equals(goodsSpecification.getSpecification())) {
                    goodsSpecifications1.add(specification);
                }
            }
            specificationListBean.setValueList(goodsSpecifications1);
            //去除重复规格信息
            if (!specificationListBeans.contains(specificationListBean)) {
                specificationListBeans.add(specificationListBean);
            }

        }
        return specificationListBeans;
    }

    private List<DetailListVO.CommentBean.CommentChildren> getCommentChildren(List<Comment> comments) {
        List<DetailListVO.CommentBean.CommentChildren> commentChildren = new ArrayList<>();
        for (Comment comment : comments) {
            DetailListVO.CommentBean.CommentChildren commentChildren1 = new DetailListVO.CommentBean.CommentChildren();
            commentChildren1.setAddTime(comment.getAddTime());
            commentChildren1.setPicList(comment.getPicUrls());
            commentChildren1.setId(comment.getId());
            commentChildren1.setContent(comment.getContent());
            //查询评论的用户头像和昵称
            User user = mallUserMapper.selectByPrimaryKey(comment.getUserId());
            if (user != null) {
                commentChildren1.setNickname(user.getNickname());
                commentChildren1.setAvatar(user.getAvatar());
            }
            //封装list信息
            commentChildren.add(commentChildren1);
        }
        return commentChildren;
    }

    /**
     * 显示相关商品信息
     * @param id
     * @return
     */
    @Override
    public List<RelatedGoods> showRelatedGoods(Integer id) {
        //查询商品所属类别
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        Integer categoryId = goods.getCategoryId();
        //查询同一类别的商品,去除他本身
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andCategoryIdEqualTo(categoryId).andIdNotEqualTo(id);
        //限制显示条数
        PageHelper.startPage(1,6);
        List<Goods> OtherGoods = goodsMapper.selectByExample(goodsExample);
        //封装
        List<RelatedGoods> goodsList = getRelatedGoods(OtherGoods);
        return goodsList;
    }

    private List<RelatedGoods> getRelatedGoods(List<Goods> otherGoods) {
        List<RelatedGoods> goodsList = new ArrayList<>();
        for (Goods otherGood : otherGoods) {
            RelatedGoods relatedGoods1 = new RelatedGoods();
            relatedGoods1.setId(otherGood.getId());
            relatedGoods1.setName(otherGood.getName());
            relatedGoods1.setBrief(otherGood.getBrief());
            relatedGoods1.setPicUrl(otherGood.getPicUrl());
            relatedGoods1.setNew(otherGood.getIsNew());
            relatedGoods1.setHot(otherGood.getIsHot());
            relatedGoods1.setCounterPrice(otherGood.getCounterPrice().intValue());
            relatedGoods1.setRetailPrice(otherGood.getRetailPrice().intValue());
            goodsList.add(relatedGoods1);
        }
        return goodsList;
    }
}
